#include <task_run.hpp>

void* TaskRun::PthreadTaskRun(void* arg)
{
	if (arg == nullptr)
	{
		return nullptr;
	}

	TaskRun* task = (TaskRun*)arg;
	task->Run(task->m_TaskArg);
	task->Stop();
	return nullptr;
}

void TaskRun::PthreadCreateTask(TaskRun* task)
{
	if (task == nullptr)
	{
		return;
	}

	pthread_t pth_id = 0;
	pthread_create(&pth_id, nullptr, TaskRun::PthreadTaskRun, task);
	pthread_detach(pth_id);
}

void TaskRun::Stop(void)
{
	m_IsRunning = false;
}

bool TaskRun::IsRunning(void)
{
	return m_IsRunning;
}

void TaskRun::mSleep(uint32_t ms)
{
	if (ms == 0)
	{
		ms = 1;
	}
	usleep(ms * 1000);
}

TaskRun::TaskRun()
	: m_IsRunning(false)
{
}

TaskRun::~TaskRun()
{
}

void TaskRun::Start(void* arg)
{
	this->m_TaskArg	  = arg;
	this->m_IsRunning = true;
	TaskRun::PthreadCreateTask(this);
}